package mo.leetcode_top;

import java.util.*;

public class L1 {

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, List<Integer>> num2index = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            List<Integer> indexes = num2index.get(nums[i]);
            if (indexes == null) {
                indexes = new ArrayList<>();
                num2index.put(nums[i], indexes);
            }
            indexes.add(i);
        }
        for (Map.Entry<Integer, List<Integer>> value : num2index.entrySet()) {
            List<Integer> left = num2index.get(target - value.getKey());
            if (left != null) {
                if (target - value.getKey() == value.getKey() && value.getValue().size() > 1) {
                    return new int[]{value.getValue().get(0), value.getValue().get(1)};
                }
                return new int[]{value.getValue().get(0), left.get(0)};
            }
        }
        return new int[]{-1, -1};
    }

    public int[] twoSumBetter(int[] nums, int target) {
        int[] indexs = new int[2];
        HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (hash.containsKey(nums[i])) {
                indexs[0] = i;
                indexs[1] = hash.get(nums[i]);
                return indexs;
            }
            hash.put(target - nums[i], i);

        }
        return indexs;
    }



    public static void main(String[] args) {
        L1 test = new L1();
        System.out.println(Arrays.toString(test.twoSum(new int[]{3, 2, 4}, 6)));
    }

}
